home *** CD-ROM | disk | FTP | other *** search
- //BUDGET6.CPP - Budget program using a template container
- // class to hold the account objects rather
- // than embed that information into the class.
-
- #include <iostream.h>
- #include <stdlib.h>
- #include <ctype.h>
-
- // use the template class SLL
- #include "sll.h"
-
- //Account - this abstract class incorporates properties
- // common to both account types Checking and
- // Savings; however, it's missing the concept
- // withdrawal() which is different between the two
- class Account
- {
- protected:
- Account(Account &c)
- {
- throw "No creating funds\n";
- }
-
- public:
- Account(unsigned accNo, float initialBalance = 0.0F);
-
- //access functions
- int accountNo()
- {
- return accountNumber;
- }
- float acntBalance()
- {
- return balance;
- }
-
- //transaction functions
- void deposit(float amount)
- {
- balance += amount;
- }
- virtual void withdrawal(float amount) = 0;
-
- //display function for displaying self on 'cout'
- void display()
- {
- cout << "Account " << accountNumber
- << " = " << balance
- << "\n";
- }
-
- protected:
- unsigned accountNumber;
- float balance;
- };
-
- Account::Account(unsigned accNo, float initialBalance)
- {
- accountNumber = accNo;
- balance = initialBalance;
- }
-
- //Checking - this class contains properties unique to
- // checking accounts. Not much left is there?
- class Checking : public Account
- {
- public:
- //here the constructor defined inline
- Checking(unsigned accNo, float initialBalance = 0.0F) :
- Account(accNo, initialBalance)
- {
- }
-
- //overload pure virtual functions
- virtual void withdrawal(float amount);
- };
-
- void Checking::withdrawal(float amount)
- {
- if (balance < amount )
- {
- cout << "Insufficient funds: balance " << balance
- << ", check " << amount
- << "\n";
- }
- else
- {
- balance -= amount;
-
- //if balance falls too low, charge service fee
- if (balance < 500.00F)
- {
- balance -= 0.20F;
- }
- }
- }
-
-
- //Savings - same story as Checking except that it also
- // has a unique data member
- class Savings : public Account
- {
- public:
- //here constructor defined as separate function
- //just to show you the difference
- Savings(unsigned accNo, float initialBalance = 0.0F);
-
- //transaction functions
- virtual void withdrawal(float amount);
-
-
- protected:
- int noWithdrawals;
- };
-
- Savings::Savings(unsigned accNo, float initialBalance) :
- Account(accNo, initialBalance)
- {
- noWithdrawals = 0;
- }
- void Savings::withdrawal(float amount)
- {
- if (balance < amount)
- {
- cout << "Insufficient funds: balance " << balance
- << ", withdrawal " << amount
- << "\n";
- }
- else
- {
- if (++noWithdrawals > 1)
- {
- balance -= 5.00F;
- }
- balance -= amount;
- }
- }
-
- //prototype declarations
- unsigned getAccntNo();
- void process(Account &account);
-
- //main - accumulate the initial input and output totals
- int main()
- {
- // loop until someone enters an 'X' or 'x'
- Account *pA;
- char accountType; //S or C
-
- try
- {
- // keep the accounts in an account container
- SLL<Account> books; // Note 1
-
- unsigned keepLooping = 1;
- while (keepLooping)
- {
- cout << "Enter S for Savings, "
- "C for Checking, X for exit\n";
- cin >> accountType;
-
- switch (accountType)
- {
- case 'c':
- case 'C':
- pA = new Checking(getAccntNo());
- if (pA == 0)
- {
- throw "Out of memory";
- }
- process(*pA);
- books.add(pA); // Note 2
- break;
-
- case 's':
- case 'S':
- pA = new Savings(getAccntNo());
- if (pA == 0)
- {
- throw "Out of memory";
- }
- process(*pA); // Note 2
- books.add(pA);
- break;
-
- case 'x':
- case 'X':
- keepLooping = 0;
- break;
-
- default:
- cout << "I didn't get that.\n";
- }
- }
-
- //now present totals
- float total = 0.0;
- cout << "Account totals:\n";
- // Note 3
- for (books.reset(); pA = books.current(); books.over())
- {
- pA->display();
- total += pA->acntBalance();
- }
- cout << "Total worth = " << total << "\n";
- }
-
- // char* exception is a general, internal error
- catch(char* pReason) // Note 4
- {
- cout << "Exception: " << pReason << endl;
- }
- catch(...)
- {
- cout << "Unknown exception\n" << endl;
- }
- return 0;
- }
-
- //getAccntNo - return the account number entered
- unsigned getAccntNo()
- {
- unsigned accntNo;
- cout << "Enter account number:";
- cin >> accntNo;
- return accntNo;
- }
-
- //process(Account) - input the data for an account*/
- void process(Account &account)
- {
- cout << "Enter positive number for deposit,\n"
- "negative for withdrawal, 0 to terminate";
-
- float transaction;
- do
- {
- cout << ":";
- cin >> transaction;
-
- //deposit
- if (transaction > 0.0F)
- {
- account.deposit(transaction);
- }
-
- //withdrawal
- if (transaction < 0.0F)
- {
- account.withdrawal(-transaction);
- }
- } while (transaction != 0.0F);
- }
-